import { create } from 'zustand';

// 定义动态存储的状态类型
type DynamicStorage = {
  set: <K extends string>(key: K, value: any) => void;
  get: <K extends string>(key: K) => any;
  remove: (key: string) => void;
  reset: (key: string) => void;
};

// 基础状态类型
type DynamicStoreState = {
  storage: Record<string, any>;
};

const useDynamicCache = create<DynamicStoreState & DynamicStorage>((set, get) => ({
  storage: {},
  set: (key: string, value: any) => set((state) => ({
    storage: { ...state.storage, [key]: value },
  })),
  get: (key: string) => {
    const state = get()
    state.storage[key]
  },
  remove: (key: string) => set((state) => {
    console.log(state.storage,'当前storage')
    return {
      storage: Object.fromEntries(Object.entries(state.storage).filter(([k]) => k !== key)),
    }
  }),
  reset: (key: string) => set((state) => {
    return {
      storage: Object.fromEntries(Object.entries(state.storage).filter(([k]) => k.startsWith(key + '$-$'))),
    }
  })
}));

export default useDynamicCache
